package com.calvin.study.controller;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.calvin.study.entity.User;

@Controller
@RequestMapping("ssoserver")
public class SsoServerController {

	@Autowired
	RedisTemplate<String, String> redisTemplate;

	@GetMapping("login.html")
	public String loginPage(@CookieValue(value = "loginToken", required = false) String loginToken,
			@RequestParam(required = false) String serviceUrl, Model model) {
		if (StringUtils.isNotBlank(loginToken)) {
			return "redirect:" + serviceUrl + "?loginToken=" + loginToken;
		}
		System.out.println(serviceUrl);
		model.addAttribute("serviceUrl", serviceUrl);
		return "login";
	}

	@PostMapping("doLogin")
	public String doLogin(HttpServletRequest request, @RequestParam String username, @RequestParam String password,
			@RequestParam(required = false) String serviceUrl, Model model, HttpServletResponse response) {

		if ("Calvin".equals(username) && "123qwe".equals(password)) {
			User user = new User();
			user.setUsername(username);
			user.setPassword(password);
			// 登录令牌
			String loginToken = UUID.randomUUID().toString().replaceAll("-", "");
			// 通过令牌获取到用户信息
			redisTemplate.opsForValue().set(loginToken, JSON.toJSONString(user), 30, TimeUnit.MINUTES);

			// 标注当前会话token，便于多个系统完成单点登录
			// request.getSession().setAttribute("loginToken", loginToken);
			Cookie cToken = new Cookie("loginToken", loginToken);
			response.addCookie(cToken);

			return "redirect:" + serviceUrl + "?loginToken=" + loginToken;// 将登录令牌返回，便于系统获取用户信息
		}
		return "redirect:/ssoserver/login.html?serviceUrl=" + serviceUrl;
	}

	// loginToken换取用户信息
	@GetMapping("/userInfo")
	public @ResponseBody User getUserInfo(@RequestParam("loginToken") String loginToken) {
		String resp = redisTemplate.opsForValue().get(loginToken);
		User user = JSON.parseObject(resp, User.class);
		return user;
	}

	public static void main(String[] args) {
		User user = new User();
		user.setUsername("cALVIN");
		user.setPassword("123qwe");
		System.out.println(JSON.toJSON(user));
		System.out.println(JSON.toJSONString(user));

		System.out.println(UUID.randomUUID().toString().replaceAll("-", ""));
	}

}
